﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SpaceMan.Physics
{
    public class PHY_Collider
    {
        public ColliderType CollisionType;
        public HitDirection CollisionAxis;
        public Vector2 Position;
        public Vector2 Offset;
        public Rectangle Rectangle;
        Vector2 Min, Max;
        public bool hitSomething, Damageable;
        bool Static, firstCycle = true;
        public DamageAffinity damageAffinity;
        float damageMultiplier;
        Action<int> Hurt;

        public void Initialize(Vector2 Position, int Width, int Height, bool Static)
        {
            CollisionType = ColliderType.Rectangle;
            Rectangle = new Rectangle((int)(Position.X), (int)(Position.Y), Width, Height);
            Offset = Position;

            Min = new Vector2(float.MinValue, float.MinValue);
            Max = new Vector2(float.MaxValue, float.MaxValue);
            this.Static = Static;
        }

        public void MakeDamageable(DamageAffinity damageAffinity, float damageMultiplier, Action<int> Hurt)
        {
            this.damageAffinity = damageAffinity;
            this.damageMultiplier = damageMultiplier;
            this.Hurt = Hurt;
            Damageable = true;
        }

        public void HurtMe(int Damage, DamageAffinity damageAffinity)
        {
            if (damageAffinity != this.damageAffinity)
                Hurt((int)(Damage * damageMultiplier));
        }

        public void Update(Vector2 Position)
        {
            Vector2 _position = Position;

            if (!firstCycle)
            {
                Position.X = MathHelper.Clamp(Position.X, Min.X, Max.X);
                Position.Y = MathHelper.Clamp(Position.Y, Min.Y, Max.Y);
            }

            else
            {
                firstCycle = false;
            }

            this.Position = Position;
            CollisionAxis = HitDirection.None;



            if (Position.X != _position.X && !Static)
            {
                hitSomething = true;
                CollisionAxis = HitDirection.Horizontal;
            }

            else if (Position.Y != _position.Y && !Static)
            {
                hitSomething = true;
                CollisionAxis = HitDirection.Vertical;
            }

            if (CollisionType == ColliderType.Circle)
            {
                this.Position = Position + Offset;
            }

            else if (CollisionType == ColliderType.Rectangle)
            {
                Rectangle = new Rectangle((int)(Position.X + Offset.X), (int)(Position.Y + Offset.Y), Rectangle.Width, Rectangle.Height);
            }

            Min = new Vector2(float.MinValue, float.MinValue);
            Max = new Vector2(float.MaxValue, float.MaxValue);
        }

        public void Clamp(Vector2 Min, Vector2 Max)
        {
            if (Min.X > this.Min.X)
            {
                this.Min.X = Min.X;
            }

            if (Max.X < this.Max.X)
            {
                this.Max.X = Max.X;
            }

            if (Min.Y > this.Min.Y)
            {
                this.Min.Y = Min.Y;
            }

            if (Max.Y < this.Max.Y)
            {
                this.Max.Y = Max.Y;
            }
        }

        

    }
}
